<?php

namespace App\Admin\Controllers;

use App\Models\Test_config;
use App\Models\Student;
use App\Models\Squad;
use App\Models\Student_mark;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Student_markController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student_mark::class, function (Grid $grid) {
            $student_id = request('student_id');
            
            // if ($student_id = request('student_id')) {
            //     $grid->model()->ofPost($student_id);
            // }
            $grid->model()->where('student_id', '=', $student_id);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            //$grid->attendance_cycle(trans('admin.attendance_cycle'))->name();
            $grid->test_id(trans('admin.test_name'))->display(function($test_id){
                return Test_config::find($this->test_id)->name;
            });
            $grid->test_batch(trans('admin.test_batch'))->editable();
            $grid->test_score(trans('admin.test_score'))->editable();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            //禁用导出按钮
            //$grid->disableExport();
            $filename="成绩单";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            $grid->disableFilter();//禁用查询
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    { 
        $form = new Form(new Student_mark());

        $form->display('id', 'ID');

        $form->select('test_id', trans('admin.test_name'))->options(Test_config::where('status','=','0')->get()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->number('test_batch', trans('admin.test_batch'))->max(10)->min(1)->addElementLabelClass('must-input');
        $form->number('test_score', trans('admin.test_score'))->max(200)->min(1)->addElementLabelClass('must-input');
        
        $form->hidden('student_id')->default(request('student_id'));
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->saved(function (Form $form) {
            $student_id = $form->model()->student_id;
            // 跳转页面
            return redirect('/admin/mark_entering/'.$student_id);
            });
        return $form;
    }

    public function test_data_make()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.create'));

            $content->body($this->test_data_form());
        });
    }

    protected function test_grid()
    {
        return Admin::grid(Student_mark::class, function (Grid $grid) {
            $grid->setResource('/admin/student_mark');
            $squad_id = request('squad_id');
            $test_id = request('test_id');
            $test_batch = request('test_batch');
            $where = [
                'squad_id' =>$squad_id,
                'test_id' =>$test_id,
                'test_batch' =>$test_batch
            ];
            $grid->model()->where($where);
           
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            });
            $grid->test_id(trans('admin.test_name'))->display(function($test_id){
                return Test_config::find($this->test_id)->name;
            });
            $grid->test_batch(trans('admin.test_batch'));
            $grid->test_score(trans('admin.test_score'))->editable();

            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableActions();

            //禁用导出按钮
            $grid->disableExport();
            $filename="成绩记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
        });
    }

    protected function test_data_form()
    { 
        $form = new Form(new Student_mark());

        $form->display('id', 'ID');
        $form->select('squad_id','要录入成绩的班级')->options(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name','id'))->load('test_id', '/api/get_squad_test')->rules('required');
        $form->select('test_id','要录入成绩的测试')->options(array(0 =>'请选择测试'));
        $form->number('test_batch', '测试批次')->max(20)->min(1);
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->setAction('test_data_save');

        return $form;
    }

    protected function test_data_save()
    {
        $squad_id = request('squad_id');
        $test_id = request('test_id');
        $test_batch = request('test_batch');
        $student_ids = Student::where('squad_id','=',$squad_id)->pluck('id')->toArray();
        $data = [];
        for ($i=0; $i <count($student_ids) ; $i++) { 
            $data[] = [
                'squad_id' =>$squad_id,
                'student_id' =>$student_ids[$i],
                'test_id' =>$test_id,
                'test_batch' =>$test_batch
            ];
        }
        $res = Student_mark::insert($data);
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.list'));
            $content->body($this->test_grid());
        });
    }
}
